package com.jfinal.code.eclipse.plugin.generator.strategy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.code.eclipse.plugin.generator.db.ConnectionDatasource;
import com.jfinal.code.eclipse.plugin.generator.db.JFinalDialectFactory;
import com.jfinal.code.eclipse.plugin.generator.dto.ValueDTO;
import com.jfinal.code.eclipse.plugin.generator.strategy.metabuilder.SqlServerMetaBuilder;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
/**
 * JFinal 代码生成策略生成代码
 * @author yaolin
 *
 */
public class JFinalGenerateStrategy implements GenerateStrategy {

	@Override
	public void generateCode(String[] tableNames, String[] selectedTableNames, ValueDTO valueDTO) {
		if (valueDTO == null || valueDTO.getDatabaseInfoValueDTO() == null || valueDTO.getJFinalValueDTO() == null) {
			return;
		}
		if (tableNames == null || tableNames.length == 0 || selectedTableNames == null || selectedTableNames.length == 0) {
			return;
		}
		
		// 获取不生成代码的表
		List<String> listExcludeTableName = new ArrayList<String>();
		boolean exclude = true;
		for (String tableName : tableNames) {
			exclude = true;
			for (String t : selectedTableNames) {
				if (tableName.equals(t)) {
					exclude = false;
					break;
				}
			}
			if (exclude) {
				listExcludeTableName.add(tableName);
			}
		}
		ConnectionDatasource connectionDatasource = new ConnectionDatasource(valueDTO.getDatabaseInfoValueDTO());
		// 组装代码生成的路径
		String codePath = valueDTO.getJFinalValueDTO().getCodePath();
		if (codePath != null) {
			codePath = codePath.replace("\\", File.separator).replace("/", File.separator);
			if (!codePath.endsWith(File.separator)) {
				codePath += File.separator;
			}
		}
		String baseModelOutputDir = codePath;
		if (valueDTO.getJFinalValueDTO().getBaseModelPackageName() != null) {
			baseModelOutputDir += valueDTO.getJFinalValueDTO().getBaseModelPackageName().replace(".", File.separator);
		}
		String modelOutputDir = codePath;
		if (valueDTO.getJFinalValueDTO().getModelPackageName() != null) {
			modelOutputDir += valueDTO.getJFinalValueDTO().getModelPackageName().replace(".", File.separator);
		}
		// 调用JFinal的Generator生成代码
		Generator generator = new Generator(connectionDatasource, valueDTO.getJFinalValueDTO().getBaseModelPackageName(), 
			baseModelOutputDir, valueDTO.getJFinalValueDTO().getModelPackageName(), modelOutputDir);
		MetaBuilder metaBuilder = null;
		// 如果是sqlServer出错使用SqlMetaBuilder
		if (valueDTO.getJFinalValueDTO().isSqlServerFixError()) {
			metaBuilder = new SqlServerMetaBuilder(connectionDatasource);
		} else {
			metaBuilder = new MetaBuilder(connectionDatasource);
		}
		metaBuilder.setDialect(JFinalDialectFactory.getDialectByDbType(valueDTO.getDatabaseInfoValueDTO().getDbType()));
		generator.setMetaBuilder(metaBuilder); // 加入方言支持
		generator.setGenerateChainSetter(false);
		generator.addExcludedTable(listExcludeTableName.toArray(new String[]{}));
		generator.setGenerateDaoInModel(valueDTO.getJFinalValueDTO().isGenDaoInModel());
		generator.setGenerateDataDictionary(valueDTO.getJFinalValueDTO().isGenDaoInModel());
		String prefix = valueDTO.getJFinalValueDTO().getTablePrefix();
		if (prefix != null && prefix.trim().length() > 0) {
			String[] tablePrefixItem = prefix.split(",");
			generator.setRemovedTableNamePrefixes(tablePrefixItem);
		}
		generator.generate();
	}
}
